package test.threads.queue.unstressed.impl;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import test.threads.queue.unstressed.IBoundedQueueFactory;
import test.threads.queue.unstressed.IQueue;

/**
 * Самый базовый вариант, адаптер {@link LinkedBlockingQueue} к интерфейсу IQueue.
 *
 * @author cheremin
 * @since 22.09.11,  13:09
 */
public class LBQWrapperQueue<T> implements IQueue<T> {

    private final LinkedBlockingQueue<T> queue;

    public LBQWrapperQueue( final int size ) {
        this.queue = new LinkedBlockingQueue<T>( size );
    }

    @Override
    public void enqueue( final T item ) throws InterruptedException {
        queue.put( item );
    }

    @Override
    public T dequeue() throws InterruptedException {
        return queue.take();
    }

    @Override
    public int size() {
        return queue.size();
    }

    @SuppressWarnings( "unchecked" )
    public static <T> IBoundedQueueFactory<T, LBQWrapperQueue<T>> factory() {
        return FACTORY;
    }

    public static final IBoundedQueueFactory FACTORY = new IBoundedQueueFactory<Object, LBQWrapperQueue<Object>>() {
        @Override
        public LBQWrapperQueue<Object> create( final int size ) {
            return new LBQWrapperQueue<Object>( size );
        }

        @Override
        public String toString() {
            return "LBQWrapperFactory";
        }
    };
}
